/*
 * Copyright (C) 2013 AndXmppSDK Project <hljdrl@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.drl.xmpp;

import java.util.ArrayList;
import java.util.List;

import org.drl.xmpp.util.StringUtils2;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * @author hljdrl@gmail.com
 * 
 */
public final class Contact implements Parcelable{
	public static final Parcelable.Creator<Contact> CREATOR = new Parcelable.Creator<Contact>() {

		@Override
		public Contact createFromParcel(Parcel source) {
			return new Contact(source);
		}

		@Override
		public Contact[] newArray(int size) {
			return new Contact[size];
		}
	};
	private int    _ID;
	private String JID;
	private String mSelectedRes;
	private List<String> mRes;
	private String Name;
	private String NickName;
	private String AvatorFile;
	private String mSign;
	private int    Status;
	private String StatusText;
	private final  List<String> mGroups = new ArrayList<String>();
	
	@Override
	public int describeContents() {
		return 0;
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(_ID);
		dest.writeInt(Status);
		dest.writeString(JID);
		dest.writeString(mSelectedRes);
		dest.writeStringList(mRes);
		dest.writeString(NickName);
		dest.writeString(getStatusText());
		dest.writeStringList(mGroups);
		dest.writeString(AvatorFile);
		dest.writeString(mSign);
	}
	
	public Contact(Parcel source) {
		_ID = source.readInt();
		Status = source.readInt();
		JID = source.readString();
		mSelectedRes = source.readString();
		mRes = new ArrayList<String>();
		source.readStringList(mRes);
		NickName = source.readString();
		setStatusText(source.readString());
		source.readStringList(mGroups);
		AvatorFile = source.readString();
		mSign = source.readString();
	}
	public Contact(String jid) {
		JID = StringUtils2.parseBareAddress2(jid);
		Status = PresenceStatus.CONTACT_STATUS_DISCONNECT;
		StatusText = null;
		mRes = new ArrayList<String>();
		String res = StringUtils.parseResource(jid);
		mSelectedRes = res;
		if (!"".equals(res))
			mRes.add(res);
	}

	public String getJID() {
		return JID;
	}
	
	public String getJIDWithRes() {
		StringBuilder build = new StringBuilder(JID);
		if (!"".equals(mSelectedRes)) {
		    build.append('/').append(mSelectedRes);
		}
		return build.toString();
    }
	
	public void setStatus(Presence presence) {
		Status = PresenceStatus.getStatusFromPresence(presence);
		setStatusText(presence.getStatus());
	}
	public void setJID(String jID) {
		JID = jID;
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public String getNickName() {
		return NickName;
	}

	public void setNickName(String nickName) {
		NickName = nickName;
	}

	public String getAvatorFile() {
		return AvatorFile;
	}

	public void setAvatorFile(String avatorFile) {
		AvatorFile = avatorFile;
	}
	
	public String getStatusText() {
		return StatusText;
	}
	public void setStatusText(String statusText) {
		StatusText = statusText;
	}

}
